#!/bin/bash

# CLI user interface
if [ "$1" == "-h" ]; then
    usage="$(basename "$0") [-h] -- program to install all the bokeh dependencies

    where:
        -h     show this help text

        -e     ENVIRONMENT were you want to install the dependencies, defaults to bokeh
        -b     install BUILD dependencies, defauls to true
        -r     install RUN dependencies, defauls to true
        -t     install TEST (and examples) dependencies, defauls to true
        -a     install ADDITIONAL image diff-related packages, defaults to false
    "
    echo "$usage"
    exit 0
fi

# defauls
env=bokeh
build=true
run=true
test=true
add=false

# handling of arguments
while getopts e:b:r:t:a option
do
    case "${option}" in
        e) env=${OPTARG};;
        b) build=${OPTARG};;
        r) run=${OPTARG};;
        t) test=${OPTARG};;
        a) add=true;;
    esac
done

# TODO: check if env exists

function get_value {
    echo $(cat <<EOF | python -
from conda_build.metadata import MetaData
print(" ".join([s.replace(" ", "") for s in MetaData("../conda.recipe").get_value("$1")]))
EOF
    )
}

function conda_install {
    channels=$(echo $(get_value 'extra/channels') | sed -e 's/^\| \+/ -c /g')
    conda install -n $env $channels --yes $@
}

if [ "$build" == "true" ]; then
    conda_install $(get_value "requirements/build")
    echo "BUILD dependecies installed."
fi

if [ "$run" == "true" ]; then
    conda_install $(get_value "requirements/run")
    echo "RUN dependecies installed."
fi

if [ "$test" == "true" ]; then
    conda_install $(get_value "test/requires")
    echo "TEST (and examples) dependecies installed."
fi

if [[ "$add" == "true" ]]; then
    conda_install pdiff boto
    echo "Image diff-related dependecies installed."
fi
